package org.ccay.manage.enumerable.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Named;

import org.ccay.core.exception.ApplicationException;
import org.ccay.core.util.StringUtil;
import org.ccay.enumerable.EnumItem;
import org.ccay.enumerable.EnumUtil;
import org.ccay.manage.enumerable.service.IEnumsQueryService;
import org.ccay.manage.enumerable.vo.EnumItemVO;

@Named
public class EnumsQueryService implements IEnumsQueryService {

	@Override
	public EnumItemVO findItem(String type, String itemCode,String language) throws ApplicationException {
		return convertToEnumItemVO(EnumUtil.getInstance().getByTypeName(type, itemCode),language);
	}

	@Override
	public List<EnumItemVO> findItemListByType(String type,String language) throws ApplicationException {
		return convertToPlainEnum(EnumUtil.getInstance().getByType(type),language);
	}
	
	private EnumItemVO convertToEnumItemVO(EnumItem item,String language) {
		EnumItemVO vo = new EnumItemVO(item,"");
		if(!StringUtil.isNullOrEmpty(language)){
			vo.setDisplayValue(item.getDisplayValue(language));
		}
		return vo;
	}
	
	private List<EnumItemVO> convertToPlainEnum(List<EnumItem> items,String language){
		List<EnumItemVO> result = new ArrayList<EnumItemVO>();
		for(EnumItem it : items){
			result.add(convertToEnumItemVO(it, language));
		}
		return result;
	}

	@Override
	public List<EnumItemVO> findItemsByType(String type, String language) throws ApplicationException {
		if(StringUtil.isNullOrEmpty(type)){
			return Collections.emptyList();
		}
		if(StringUtil.isNullOrEmpty(language)){
			language = "zh_CN";
		}
		return findItemListByType(type,language);
	}
}
